------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[

]]--
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
local assets = {
    -- Asset("IMAGE", "images/inventoryimages/spell_reject_the_npc.tex"),
	-- Asset("ATLAS", "images/inventoryimages/spell_reject_the_npc.xml"),
	-- Asset("ANIM", "anim/loramia_fx_red_bats.zip"),
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--
    local SCORCH_BLUE_FRAMES = 20
    local SCORCH_DELAY_FRAMES = 0
    local SCORCH_FADE_FRAMES = 5
    local function Scorch_OnFadeDirty(inst,value)
        --V2C: hack alert: using SetHightlightColour to achieve something like OverrideAddColour
        --     (that function does not exist), because we know this FX can never be highlighted!
        if value > SCORCH_FADE_FRAMES + SCORCH_DELAY_FRAMES then
            local k = (value - SCORCH_FADE_FRAMES - SCORCH_DELAY_FRAMES) / SCORCH_BLUE_FRAMES
            inst.AnimState:OverrideMultColour(1, 1, 1, 1)
            -- inst.AnimState:SetHighlightColour(0, 0, k, 0)
            inst.AnimState:SetHighlightColour(inst.r * k, inst.g * k, inst.b * k, 0)
        elseif value >= SCORCH_FADE_FRAMES then
            inst.AnimState:OverrideMultColour(1, 1, 1, 1)
            inst.AnimState:SetHighlightColour()
        else
            local k = value / SCORCH_FADE_FRAMES
            k = k * k
            inst.AnimState:OverrideMultColour(1, 1, 1, k)
            inst.AnimState:SetHighlightColour()
        end
    end
    local function Scorch_OnUpdateFade(inst)
        if inst.fade_value > 1 then
            inst.fade_value = inst.fade_value - 1
            Scorch_OnFadeDirty(inst,inst.fade_value)
        elseif inst.fade_value > 0 then
            inst.AnimState:OverrideMultColour(1, 1, 1, 0)
        end
        if inst.fade_value <= 0 then
            inst:Remove()
        end
    end
    local function CreateGroundFx(parent)
        --------------------------------------------------------------------
        --- 配置颜色
            local colour_array = parent.__net_colour:value()
            local r = colour_array[1]/255
            local g = colour_array[2]/255
            local b = colour_array[3]/255
        --------------------------------------------------------------------
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddNetwork()
    
        inst.AnimState:SetBuild("burntground")
        inst.AnimState:SetBank("burntground")
        inst.AnimState:PlayAnimation("idle")
        inst.AnimState:SetOrientation(ANIM_ORIENTATION.OnGround)
        inst.AnimState:SetLayer(LAYER_BACKGROUND)
        inst.AnimState:SetSortOrder(3)
    
        inst:AddTag("NOCLICK")
        inst:AddTag("FX")

        inst.r = r or 255/255
        inst.g = g or 0/255
        inst.b = b or 0/255

        inst.fade_value = SCORCH_BLUE_FRAMES + SCORCH_DELAY_FRAMES + SCORCH_FADE_FRAMES
        inst:DoPeriodicTask(0, Scorch_OnUpdateFade)
        Scorch_OnFadeDirty(inst,inst.fade_value)
        inst.Transform:SetRotation(math.random() * 360)
        -- inst.entity:SetParent(parent.entity)
        inst.Transform:SetPosition(parent.Transform:GetWorldPosition())
        return inst
    end
------------------------------------------------------------------------------------------------------------------------
local function fx()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddNetwork()
    inst.entity:AddSoundEmitter()

    inst:AddTag("FX")
    inst:AddTag("NOCLICK")

    inst.AnimState:SetBank("lavaarena_staff_smoke_fx")
    inst.AnimState:SetBuild("lavaarena_staff_smoke_fx")
    inst.AnimState:PlayAnimation("idle")
    inst.AnimState:SetAddColour(0, 0, 1, 0)
    inst.AnimState:SetMultColour(0, 0, 1, 1)
    inst.AnimState:SetBloomEffectHandle("shaders/anim.ksh")
    inst.entity:SetPristine()

    inst.__net_colour = net_bytearray(inst.GUID,"loramia_single_laser_beam","colour")
    inst:ListenForEvent("animover",inst.Remove)
    if not TheNet:IsDedicated() then
        inst:DoTaskInTime(0,CreateGroundFx)
    end

    if not TheWorld.ismastersim then
        return inst
    end

    -- inst.components.colouradder:OnSetColour(139/255,34/255,34/255,0.1)
    inst:ListenForEvent("Set",function(inst,_table)
        -- _table = {
        --     pt = Vector3(0,0,0),
        --     target = inst, 
        --     colour = {139,34,34},
        --     speed = 1,
        --     sound = "",
        -- }
        if _table == nil then
            return
        end
        if _table.pt and _table.pt.x then
            inst.Transform:SetPosition(_table.pt.x,_table.pt.y,_table.pt.z)
        end
        if _table.target then
            inst.Transform:SetPosition(_table.target.Transform:GetWorldPosition())
        end
        if type(_table.colour) == "table" and _table.colour[1] and _table.colour[2] and _table.colour[3] then
            local r,g,b = _table.colour[1],_table.colour[2],_table.colour[3]
            inst.__net_colour:set({r,g,b})
            inst.AnimState:SetAddColour(r/255, g/255, b/255, 0)
            inst.AnimState:SetMultColour(r/255, g/255, b/255, 1)
        end
        if _table.sound then
            inst.SoundEmitter:PlaySound(_table.sound)
        end        
        inst.Ready = true
    end)

    inst:DoTaskInTime(0,function()
        if inst.Ready ~= true then
            inst:Remove()
        end
    end)

    return inst
end

return Prefab("loramia_sfx_single_laser_beam",fx,assets)